<template>
<div>
     <!-- 面包屑 -->
 <el-breadcrumb separator-class="el-icon-arrow-right">
  <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
  <el-breadcrumb-item>商品管理</el-breadcrumb-item>
  <el-breadcrumb-item>参数列表</el-breadcrumb-item>
</el-breadcrumb>

<el-card>
<el-alert
show-icon
:closable="false"
    title="注意：只允许为第三级分类设置相关参数！"
    type="warning"
    effect="dark">
</el-alert>
<p>选择商品分类:
   <el-cascader
    v-model="value1"
    :options="spflList"
    :props="spflprops"
    @change="handleChange">
    </el-cascader>
</p>

<el-tabs v-model="value2" @tab-click="tabclick">
    <el-tab-pane label="动态参数" name='many'>
    <el-button size="mini" type="primary" :disabled="isdisable" @click="addForm1">添加参数</el-button>
    <el-table :data="dtcsList" style="width: 100%">
      <el-table-column type="expand">
       <template slot-scope="scope">
          <el-tag
        :key="i"
        v-for="(item, i) in scope.row.attr_vals"
        closable
        :disable-transitions="false"
        @close="handleClose(i, scope.row)">
        {{item}}
      </el-tag>
      <el-input
        class="input-new-tag"
        v-if="scope.row.inputVisible"
        v-model="scope.row.inputValue"
        ref="saveTagInput"
        size="small"
        @keyup.enter.native="handleInputConfirm(scope.row)"
        @blur="handleInputConfirm(scope.row)">
      </el-input>
       <el-button v-else class="button-new-tag" size="small" @click="showInput(scope.row)">+ New Tag</el-button>
      </template>
      </el-table-column>
      <el-table-column type="index"></el-table-column>
      <el-table-column prop="attr_name" label="参数名称"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="xiugai(scope.row.attr_id)">修改</el-button>
        <el-button type="danger" icon="el-icon-delete" size="mini" @click="shanchu(scope.row.attr_id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    </el-tab-pane>
    <el-tab-pane label="静态属性" name="only">
    <el-button size="mini" type="primary" :disabled="isdisable" @click="addForm1">添加属性</el-button>
    <el-table :data="jtsxList" style="width: 100%">
      <el-table-column type="expand">
           <template slot-scope="scope">
          <el-tag
        :key="i"
        v-for="(item, i) in scope.row.attr_vals"
        closable
        :disable-transitions="false"
        @close="handleClose(i, scope.row)">
        {{item}}
      </el-tag>
      <el-input
        class="input-new-tag"
        v-if="scope.row.inputVisible"
        v-model="scope.row.inputValue"
        ref="saveTagInput"
        size="small"
        @keyup.enter.native="handleInputConfirm(scope.row)"
        @blur="handleInputConfirm(scope.row)">
      </el-input>
       <el-button v-else class="button-new-tag" size="small" @click="showInput(scope.row)">+ New Tag</el-button>
      </template>
      </el-table-column>
      <el-table-column type="index"></el-table-column>
      <el-table-column prop="attr_name" label="属性名称"></el-table-column>
      <el-table-column label="操作">
         <template slot-scope="scope">
        <el-button type="primary" icon="el-icon-edit" size="mini" @click="xiugai(scope.row.attr_id)">修改</el-button>
        <el-button type="danger" icon="el-icon-delete" size="mini" @click="shanchu(scope.row.attr_id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    </el-tab-pane>
  </el-tabs>
</el-card>

<!-- 添加参数属性对话框 -->
<el-dialog :title="'添加' + title" :visible.sync="addFormVisible" width="50%" @close="guanbi">
  <el-form label-width="80px" :model="tianjiaForm" :rules="ruless" ref="addsFormRef">
    <el-form-item :label="title" prop="attr_name">
    <el-input v-model="tianjiaForm.attr_name"></el-input>
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="guanbi">取 消</el-button>
    <el-button type="primary" @click="addbtn()">确 定</el-button>
  </div>
</el-dialog>

<!-- 修改参数属性对话框 -->
<el-dialog :title="'修改' + title" :visible.sync="xgFormVisible" width="50%" @close="guanbi2">
  <el-form label-width="80px" :model="xiugaiForm" :rules="ruless" ref="xgFormRef">
    <el-form-item :label="title" prop="attr_name">
    <el-input v-model="xiugaiForm.attr_name"></el-input>
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="guanbi2">取 消</el-button>
    <el-button type="primary" @click="xgbtn()">确 定</el-button>
  </div>
</el-dialog>
</div>
</template>

<script>
export default {
  data () {
    return {
      spflList: [],
      value1: [],
      spflprops: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        expandTrigger: 'hover'
      },
      canshuList: [],
      isdisable: true,
      value2: 'many',
      dtcsList: [],
      jtsxList: [],
      addFormVisible: false,
      tianjiaForm: {
        attr_name: ''
      },
      attr_name: '',
      title: '',
      xiugaiForm: {},
      xgFormVisible: false,
      ruless: {
        attr_name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.getspflList()
  },
  methods: {
    async getspflList () {
      const { data: res } = await this.$http.get('categories', { params: { type: 3 } })
      if (res.meta.status !== 200) return this.$message.error('获取商品分类列表数据失败')
      this.spflList = res.data
    },
    handleChange () {
      this.getList()
    },
    async getList () {
      if (this.value1.length !== 3) {
        this.value1 = []
        this.isdisable = true
        this.canshuList = []
        return this.value1
      }
      this.isdisable = false
      const { data: res } = await this.$http.get(`categories/${this.cateId}/attributes`, { params: { sel: this.value2 } })
      if (res.meta.status !== 200) return this.$message.error('获取动态参数列表数据失败')

      res.data.forEach(item => {
        item.attr_vals = item.attr_vals ? item.attr_vals.split(' ') : []
        item.inputValue = ''
        item.inputVisible = false
      })
      if (this.value2 === 'many') {
        this.dtcsList = res.data
        this.title = '动态参数'
        return this.dtcsList
      } else {
        this.jtsxList = res.data
        this.title = '静态属性'
        return this.jtsxList
      }
    },
    tabclick () {
      this.getList()
    },
    async handleClose (i, row) {
      row.attr_vals.splice(i, 1)
      const { data: res } = await this.$http.put(`categories/${this.cateId}/attributes/${row.attr_id}`, { attr_name: row.attr_name, attr_sel: row.attr_sel, attr_vals: row.attr_vals.join(' ') })
      if (res.meta.status !== 200) return this.$message.error('动态参数删除失败')
      this.$message.success('动态参数删除成功')
    },
    async handleInputConfirm (row) {
      if (row.inputValue.trim().length === 0) {
        row.inputValue = ''
        row.inputVisible = false
        return
      }
      row.attr_vals.push(row.inputValue.trim())
      row.inputValue = ''
      row.inputVisible = false
      const { data: res } = await this.$http.put(`categories/${this.cateId}/attributes/${row.attr_id}`, { attr_name: row.attr_name, attr_sel: row.attr_sel, attr_vals: row.attr_vals.join(' ') })
      if (res.meta.status !== 200) return this.$message.error('动态参数创建失败')
      this.$message.success('动态参数创建成功')
    },
    showInput (row) {
      row.inputVisible = true
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },
    addForm1 () {
      this.addFormVisible = true
    },
    guanbi () {
      this.$refs.addsFormRef.resetFields()
      this.addFormVisible = false
    },
    guanbi2 () {
      this.$refs.xgFormRef.resetFields()
      this.xgFormVisible = false
    },
    addbtn () {
      this.$refs.addsFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post(`categories/${this.cateId}/attributes`, { attr_name: this.tianjiaForm.attr_name, attr_sel: this.value2 })
        if (res.meta.status !== 201) return this.$message.error('添加数据失败')
        this.getList()
        this.addFormVisible = false
      })
    },
    async xiugai (id) {
      const { data: res } = await this.$http.get(`categories/${this.cateId}/attributes/${id}`, { params: { attr_sel: this.value2 } })
      if (res.meta.status !== 200) return this.$message.error('获取参数失败')
      this.xiugaiForm = res.data
      this.xgFormVisible = true
    },
    xgbtn () {
      this.$refs.xgFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.put(`categories/${this.cateId}/attributes/${this.xiugaiForm.attr_id}`, { attr_name: this.xiugaiForm.attr_name, attr_sel: this.value2 })
        if (res.meta.status !== 200) return this.$message.error('修改数据失败')
        this.getList()
        this.xgFormVisible = false
      })
    },
    async shanchu (id) {
      const result = await this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
      if (!result) return
      const { data: res } = await this.$http.delete(`categories/${this.cateId}/attributes/${id}`)
      if (res.meta.status !== 200) return this.$message.error('删除角色失败')
      this.$message.success('删除角色成功')
      this.getList()
    }
  },
  computed: {
    cateId () {
      if (this.value1.length === 3) {
        return this.value1[2]
      }
      return null
    }
  }
}
</script>

<style scoped lang="less">
.input-new-tag {
  width: 150px;
}
.el-tag {
  margin: 10px;
}
</style>
